<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }


Class IndexController Extends baseController {

	/**
	 * default action
	 * 
	 * @param string $id
	 */
    public function index($user, $id = null) 
    {
        //$user = $this->registry->db->get('userManager')->getCurrentUser();
        
        if(!$user){
        	header('Location: '.BSTO_PATH.'/login');
        }
    	    	
    	$userType = $user->get('type');
    	$userName = $user->get('username');
    	if($userType == 'Admin')
    	{
    		$tags = array('bstoContentTitle' => 'Welcome Administrator !',);
    						//'username' => $userName);
    	}
    	else 
    		$tags = array('bstoContentTitle' => 'Welcome '.$userName.' !',);
        					//'username' => $userName);
        					
        $content = <<<EOT
Welcome to Book Store Control Panel !<br />
<br /><br />
EOT;

		$fullOption = $this->registry->template->getTableSelectOption();
		$type = $user->get('type');
		
		if($user){
			
			if($type == "Normal"){
				$tags['bstoTableSelectOption'] = '';
			}
			else if($type == "Admin"){
				$tags['bstoTableSelectOption'] = $fullOption;
			}
		}
		else {
			exit();
		}
			
		if(!isset($_POST['submit'])){
			if($type == "Admin")
				$content .= $this->registry->template->getModule('search', $tags);
	        $output = $this->registry->template->getPageContent('index', 'BSTO Administrator CP', $content, $tags);
	        
	        echo $output; 
		}
		else {
			
			if($type == "Admin")
				$content .= $this->registry->template->getModule('search', $tags);
			
			$table = mysql_real_escape_string($_POST['table_select']);
			$attr  = mysql_real_escape_string($_POST['attr_select']);
			$value = mysql_real_escape_string($_POST['search_value']);
			
			$results = $this->registry->db->searchData($table, $attr, $value);
			
			if(!$results){
				$content .= '<div class="redFont"> > Your search "<b>' . $value . '</b>" - did not match any "<b>' . $table . ' : '. $attr .'</b>". </div>';
			}
			else {
				$tableid 	= array('book' 		=> 'nid',
									'category' 	=> 'cid',
									'config'	=> 'id',
									'static'	=> 'nid',
									'user'		=> 'uid');
				$rid = $tableid[$table];
				
				// build UI to result
				$tmpContent = $this->registry->template->getModule('searchResult', $tags);
				
				$id = 0;
				$strContent = '';
				$strContentTotal = '';

				// convert to 2 arguments with book, static, config page
				if(in_array($table, array('book', 'static', 'config'))){
					$forBookArg = '1/';
				}
				else {
					$forBookArg = '';
				}
				
				$editTemplate = '<a href="{path}/edit/'.$forBookArg.'{bstoId}" class="icon-1 info-tooltip"></a>';
				$deleteTemplate = '<a href="{path}/delete/'.$forBookArg.'{bstoId}" class="icon-2 info-tooltip"></a>';
				
				if($table == 'user'){
					$optionTemplate = $deleteTemplate;
				}
				else if($table == 'static' || $table == 'config'){
					$optionTemplate = $editTemplate;
				}
				else {
					$optionTemplate = $editTemplate . $deleteTemplate;
				}
				
				foreach($results as $result){
			   		if( $id % 2 == 0 ) {
			   			$strContent = str_replace('{class}', '', $tmpContent);
			   		} else {
			   			$strContent = str_replace('{class}', 'alternate-row', $tmpContent);
			   		}
			   		$strContent = str_replace('{bstoOption}', $optionTemplate, $strContent);
			   		$strContent = str_replace('{bstoId}', $result->get($rid), $strContent);
			   		$strContent = str_replace('{bstoAttr}', $result->get($attr), $strContent);
	
			   		$strContentTotal = $strContentTotal . $strContent;
				}

				
				$strContentTotal = str_replace('{path}', $this->registry->router->getRoot(). $table ,$strContentTotal);
				
				$tableContent = $this->registry->template->getModule('searchResults', array('bstoTableContent' => $strContentTotal, 'searchAttr' => $attr));
				
				$content .= $tableContent;
			}
			
			$script = <<<EOT
<script type="text/javascript">
$(document).ready(function() {
	$("#table_select").val('$table');
	bstoMenuSelect('');
	$("#attr_select").val('$attr');
	$("#search_value").val('$value');

});
</script>		
EOT;
			$output = $this->registry->template->getPageContent('index', 'BSTO ACP - Search Results', $content . $script, $tags);
        
        
        	echo $output;
			}
    }  
}
?>
